//
//  VisitorView.swift
//  weibo1990
//
//  Created by 曹晓龙 on 15/10/17.
//  Copyright © 2015年 曹晓龙. All rights reserved.
//

import UIKit

import SnapKit

class VisitorView: UIView {

    ///  设置各个界面未登录按钮的文字和图片
    ///
    ///  - parameter imageName: 图片名
    ///  - parameter title:     文字内容
    func setupView(imageName:String?, title:String){
        
        titleLbl.text = title
        
        if imageName == nil {
  
            anim()
            
            return
        }
        
        homeView.hidden = true
        
        sendSubviewToBack(coverIcon)
        
        iconView.image = UIImage(named: imageName!)
        
    }
    

    private func anim(){
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * M_PI
        
        anim.repeatCount = MAXFLOAT
        
        anim.duration = 20
        
        anim.removedOnCompletion = false
        
        iconView.layer.addAnimation(anim, forKey: nil)
    
    }
    
    // MARK: -构造方法
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        // 添加子视图
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    // MARK:- 懒加载子控件
    // 小图片
    private lazy var iconView:UIImageView = UIImageView(imageName: "visitordiscover_feed_image_smallicon")
    // 遮挡图片
    private lazy var coverIcon:UIImageView = UIImageView(imageName: "visitordiscover_feed_mask_smallicon")
    // 小房子
    private lazy var homeView:UIImageView = UIImageView(imageName: "visitordiscover_feed_image_house")
    //文字
    private lazy var titleLbl:UILabel = UILabel(titleText: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜", textFont: 14)
    // 注册按钮
    lazy var registerBtn:UIButton = UIButton(title: "注册", backImageName: "common_button_white_disable", titleColor: UIColor.orangeColor())
    //登录按钮
    lazy var loginBtn:UIButton = UIButton(title: "登录", backImageName: "common_button_white_disable")

}

extension VisitorView {
   
    private func setupUI(){
        
        // 添加子视图
        addSubview(iconView)
        addSubview(coverIcon)
        addSubview(homeView)
        addSubview(titleLbl)
        addSubview(registerBtn)
        addSubview(loginBtn)
        //MARK: - 设置子视图自动布局
        
        backgroundColor = UIColor(white: 233/255.0, alpha: 1.0)
        // 图标
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-60)
        }
        
        // 小房子
        homeView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(iconView.snp_center)
        }
        
        // 文字
        titleLbl.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(16)
            make.width.equalTo(266)
            make.height.equalTo(36)
        }
        
        //注册按钮
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(titleLbl.snp_left)
            make.top.equalTo(titleLbl.snp_bottom).offset(16)
            make.width.equalTo(100)
            make.height.equalTo(36)
        }
        //登录按钮
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(titleLbl.snp_right)
            make.size.equalTo(registerBtn.snp_size)
            make.top.equalTo(registerBtn.snp_top)
        }
        
        // 遮挡
        coverIcon.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
            make.top.equalTo(self.snp_top)
            make.bottom.equalTo(registerBtn.snp_top)
        }
    }
    
}